package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.event.PayResetEvent;
import com.qf.flash.model.MyLaunchBean;
import com.qf.flash.mvp.contract.MyLanchedActContract;
import com.qf.flash.mvp.presenter.MyLanchedActPresenter;
import com.qf.flash.ui.home.adapter.MyLaunchItem;
import com.qf.flash.utils.MyUtil;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我发起的activity
 */
public class CommonRecyclerActivity extends BaseActivity implements MyLanchedActContract.View, OnRefreshListener, OnLoadMoreListener {
    private static String TYPE = "type";
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.rl_choose_all)
    RelativeLayout rlChooseAll;
    @BindView(R.id.tv_kong)
    TextView tvKong;
    private RVAdapter<Object> mAdapter;
    private MyLanchedActPresenter lanchedActPresenter;
    private int num = 1, size = 30;
    private int intExtra;//1.我发起的   2.我加入的
    private boolean isfresh,isLoadMore;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_launch;
    }

    @Override
    protected void initViewAndData() {
        rlChooseAll.setVisibility(View.GONE);
        intExtra = getIntent().getIntExtra(TYPE, 0);
        switch (intExtra) {
            case 1:
                tvTopTitle.setText("我发起的组队");
                tvTopRight.setVisibility(View.GONE);
                break;
            case 2:
                tvTopTitle.setText("我参与的组队");
                tvTopRight.setVisibility(View.GONE);
                break;
        }

        initRvAdapter(intExtra);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mRefresh.autoRefresh();
    }


    private void initRvAdapter(int intExtra) {
        switch (intExtra) {
            case 1:
                mAdapter = new RVAdapter<>(new MyLaunchItem(1));
                break;
            case 2:
                mAdapter = new RVAdapter<>(new MyLaunchItem(2));
                break;

        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);

    }

    public static void start(Activity activity, int type) {
        Intent intent = new Intent(activity, CommonRecyclerActivity.class);
        intent.putExtra(TYPE, type);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_top_right:

                break;
        }
    }

    @Override
    protected void initInject() {
        super.initInject();
        lanchedActPresenter = new MyLanchedActPresenter();
        initPresenters(lanchedActPresenter);
    }

    @Override
    public void setLaunchBean(List<MyLaunchBean> myLaunchBeans, int totalPage) {
        mRefresh.setEnableLoadMore(num < totalPage);
        if (isfresh) {
            mRefresh.finishRefresh();
        } else if(isLoadMore){
            mRefresh.finishLoadMore();
        }
        if(MyUtil.isNullOrEmpty(myLaunchBeans)){
            tvKong.setVisibility(View.VISIBLE);
            return;
        }else {
            tvKong.setVisibility(View.GONE);
        }
        List<Object> datas = mAdapter.getDatas();
        if (isfresh) {
            datas.clear();
            datas.addAll(myLaunchBeans);
        } else {
            datas.addAll(myLaunchBeans);
        }
        mAdapter.notifyDataSetChanged();
        isfresh = false;
        isLoadMore = false;
    }

    @Override
    public void setMyJoinedGame(List<MyLaunchBean> myJoinBeans, int totalPage) {
        mRefresh.setEnableLoadMore(num < totalPage);
        List<Object> datas = mAdapter.getDatas();
        if (isfresh) {
            datas.clear();
            mRefresh.finishRefresh();
        } else if(isLoadMore){
            mRefresh.finishLoadMore();
        }
        if(MyUtil.isNullOrEmpty(myJoinBeans)){
            tvKong.setVisibility(View.VISIBLE);
            return;
        }else {
            tvKong.setVisibility(View.GONE);
        }

        datas.addAll(myJoinBeans);

        mAdapter.notifyDataSetChanged();
        isfresh = false;
        isLoadMore = false;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isfresh = true;
        num = 1;
        switch (intExtra) {
            case 1:
                lanchedActPresenter.getMyLanchedAct(num, size);
                break;
            case 2:
                lanchedActPresenter.getMyJoinGame(num, size);
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isLoadMore = true;
        num++;
        switch (intExtra) {
            case 1:
                lanchedActPresenter.getMyLanchedAct(num, size);
                break;
            case 2:
                lanchedActPresenter.getMyJoinGame(num, size);
                break;
        }
    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    @Subscribe
    public void onResetPayEvent(PayResetEvent payResetEvent) {
        mRefresh.autoRefresh();
    }
}
